﻿namespace PrismEx.UnityExtensions.Tests.Steps
{
    using System;

    using FluentAssertions;

    using Microsoft.Practices.Prism.Events;
    using Microsoft.Practices.Unity;

    using NUnit.Framework;

    using PrismEx.Events;
    using PrismEx.Interfaces;
    using PrismEx.UnityExtensions.Tests.TestDomain;

    using TechTalk.SpecFlow;

    [Binding]
    public class AutoSubscriptionSteps
    {
        private SingleTypeResolutionTestDomain<Foo, EventStub> testDomain;

        [Given(@"a unity container has been created")]
        public void GivenAUnityContainerHasBeenCreated()
        {
            testDomain = new SingleTypeResolutionTestDomain<Foo, EventStub>();
        }

        [Given(@"AutoSubscribeUnityContainer has been registered")]
        public void GivenAutoSubscribeUnityContainerHasBeenRegistered()
        {
            testDomain.RegisterContainerExtension(new AutoSubscribeUnityContainerExtension(testDomain.EventAggregator));
        }

        [When(@"a type is resolved which implements IAutoSubscribe<""SomeType"">")]
        public void WhenATypeIsResolvedWhichImplementsIAutoSubscribeSomeType()
        {
           testDomain.ResolveInstance();
        }

        [When(@"""SomeType"" event is published")]
        public void WhenSomeTypeEventIsPublished()
        {
            testDomain.EventAggregator.Publish(new EventStub() { Message = "Hello World" });
        }

        [Then(@"the subscription should have called the delegate")]
        public void ThenTheSubscriptionShouldHaveCalledTheDelegate()
        {
            testDomain.SubscriptionDelegateCallCount.Should().Be(1);
        }

        public class EventStub : CompositePresentationEvent<EventStub>
        {
            public string Message { get; set; }
        }

        public class Foo : IAutoSubscribe<EventStub>
        {
            [Dependency]
            public Action<EventStub> Handler { get; set; }

            public void Unsubscribe()
            {
                throw new System.NotImplementedException();
            }
        }
    }
}
